package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.HomeCollect;
import com.ruoyi.system.domain.HomeHistory;
import com.ruoyi.system.domain.vo.HomeVO;
import com.ruoyi.system.domain.vo.SysListingInformationDetailVO;
import com.ruoyi.system.domain.vo.SysListingInformationListVO;
import com.ruoyi.system.domain.vo.SysListingInformationVO;
import com.ruoyi.system.mapper.HomeCollectMapper;
import com.ruoyi.system.mapper.HomeHistoryMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysListingInformationMapper;
import com.ruoyi.system.domain.SysListingInformation;
import com.ruoyi.system.service.ISysListingInformationService;

import static com.ruoyi.common.utils.PageUtils.getDataTable;
import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 房源Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-01
 */
@Service
public class SysListingInformationServiceImpl extends ServiceImpl<SysListingInformationMapper, SysListingInformation> implements ISysListingInformationService {
    @Autowired
    private SysListingInformationMapper sysListingInformationMapper;

    @Autowired
    private HomeHistoryMapper homeHistoryMapper;

    @Autowired
    private HomeCollectMapper homeCollectMapper;

    /**
     * 查询房源
     *
     * @param homeVO 房源主键
     * @return 房源
     */
    @Override
    public SysListingInformationDetailVO selectSysListingInformationByListingId(HomeVO homeVO) {

        //判断当前用户是否登录
        if (ObjectUtils.isNotEmpty(homeVO.getUserId())){
            //添加浏览记录
            LambdaQueryWrapper<HomeHistory> homeHistoryLambdaQueryWrapper=new LambdaQueryWrapper<>();
            homeHistoryLambdaQueryWrapper.eq(HomeHistory::getHomeId,homeVO.getListingId());
            homeHistoryLambdaQueryWrapper.eq(HomeHistory::getUserId,homeVO.getUserId());
            HomeHistory homeHistory = homeHistoryMapper.selectOne(homeHistoryLambdaQueryWrapper);
            if (ObjectUtils.isNotEmpty(homeHistory)){
                homeHistoryMapper.deleteById(homeHistory.getId());
            }

            homeHistory=new HomeHistory();
            homeHistory.setHomeId(homeVO.getListingId());
            homeHistory.setUserId(homeVO.getUserId());
            homeHistory.setCreateTime(new Date());
            homeHistoryMapper.insert(homeHistory);
        }


        SysListingInformationDetailVO detailVO=new SysListingInformationDetailVO();
        SysListingInformation sysListingInformation = sysListingInformationMapper.selectById(homeVO.getListingId());
        BeanUtils.copyProperties(sysListingInformation,detailVO);
        if (ObjectUtils.isNotEmpty(sysListingInformation.getImages())) {
            List<String> list = JSON.parseArray(sysListingInformation.getImages(), String.class);
            detailVO.setImageList(list);
        }
        //用户是否关注
        if (ObjectUtils.isNotEmpty(homeVO.getUserId())){
            LambdaQueryWrapper<HomeCollect> homeCollectLambdaQueryWrapper=new LambdaQueryWrapper<>();
            homeCollectLambdaQueryWrapper.eq(HomeCollect::getHomeId,homeVO.getListingId());
            homeCollectLambdaQueryWrapper.eq(HomeCollect::getUserId,homeVO.getUserId());
            HomeCollect homeCollect = homeCollectMapper.selectOne(homeCollectLambdaQueryWrapper);
            if (ObjectUtils.isNotEmpty(homeCollect)){
                detailVO.setAttentionStatus(1);
            }else {
                detailVO.setAttentionStatus(0);
            }
        }else {
            detailVO.setAttentionStatus(0);
        }

        LambdaQueryWrapper<SysListingInformation> sysListingInformationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysListingInformationLambdaQueryWrapper.ne(SysListingInformation::getId,homeVO.getListingId());
        sysListingInformationLambdaQueryWrapper.eq(SysListingInformation::getHomeStatus,0);
        List<SysListingInformation> list = sysListingInformationMapper.selectList(sysListingInformationLambdaQueryWrapper);
        List<SysListingInformationListVO> listVO = list.stream().map(x -> {
            SysListingInformationListVO vo = new SysListingInformationListVO();
            BeanUtils.copyProperties(x, vo);
            if (ObjectUtils.isNotEmpty(x.getImages())) {
                List<String> imageList = JSON.parseArray(x.getImages(), String.class);
                vo.setImage(imageList.get(0));
            }
            return vo;
        }).collect(Collectors.toList());

        detailVO.setOtherRoom(listVO);
        return detailVO;
    }

    /**
     * 查询房源列表
     *
     * @return 房源
     */
    @Override
    public TableDataInfo selectSysListingInformationList() {
//        return sysListingInformationMapper.selectSysListingInformationList(sysListingInformation);
        startPage();
        LambdaQueryWrapper<SysListingInformation> sysListingInformationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysListingInformationLambdaQueryWrapper.eq(SysListingInformation::getHomeStatus,0);
        sysListingInformationLambdaQueryWrapper.orderByDesc(SysListingInformation::getCreateTime);
        List<SysListingInformation> list = this.list(sysListingInformationLambdaQueryWrapper);

        long total = new PageInfo<>(list).getTotal();
        List<SysListingInformationListVO> listVO = list.stream().map(x -> {
            SysListingInformationListVO vo = new SysListingInformationListVO();
            System.out.println(x.getId());
            BeanUtils.copyProperties(x, vo);
            vo.setId(x.getId());
            if (ObjectUtils.isNotEmpty(x.getImages())) {
                List<String> imageList = JSON.parseArray(x.getImages(), String.class);
                vo.setImage(imageList.get(0));
            }
            return vo;
        }).collect(Collectors.toList());

        return getDataTable(listVO,total);
    }

    /**
     * 新增房源
     *
     * @param sysListingInformationVO 房源
     * @return 结果
     */
    @Override
    public void insertSysListingInformation(SysListingInformationVO sysListingInformationVO) {
        SysListingInformation sysListingInformation = new SysListingInformation();
        BeanUtils.copyProperties(sysListingInformationVO, sysListingInformation);
        String jsonString = JSON.toJSONString(sysListingInformationVO.getImageList());
        sysListingInformation.setImages(jsonString);
        sysListingInformation.setCreateTime(new Date());
        sysListingInformation.setHomeUserId(sysListingInformationVO.getUserId());
        sysListingInformationMapper.insert(sysListingInformation);
    }

    /**
     * 修改房源
     *
     * @param sysListingInformation 房源
     * @return 结果
     */
    @Override
    public int updateSysListingInformation(SysListingInformation sysListingInformation) {
        return sysListingInformationMapper.updateSysListingInformation(sysListingInformation);
    }

    /**
     * 批量删除房源
     *
     * @param listingIds 需要删除的房源主键
     * @return 结果
     */
    @Override
    public int deleteSysListingInformationByListingIds(Long[] listingIds) {
        return sysListingInformationMapper.deleteSysListingInformationByListingIds(listingIds);
    }

    /**
     * 删除房源信息
     *
     * @param listingId 房源主键
     * @return 结果
     */
    @Override
    public int deleteSysListingInformationByListingId(Long listingId) {
        return sysListingInformationMapper.deleteSysListingInformationByListingId(listingId);
    }

    @Override
    public TableDataInfo homeList() {
        startPage();
        LambdaQueryWrapper<SysListingInformation> sysListingInformationLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysListingInformationLambdaQueryWrapper.orderByDesc(SysListingInformation::getCreateTime);
        List<SysListingInformation> list = this.list(sysListingInformationLambdaQueryWrapper);

        long total = new PageInfo<>(list).getTotal();
        List<SysListingInformationDetailVO> voList=list.stream().map(x->{

            SysListingInformationDetailVO detailVO = BeanUtil.toBean(x, SysListingInformationDetailVO.class);
            List<String> imageList = JSON.parseArray(x.getImages(), String.class);
            detailVO.setImageList(imageList);
            return detailVO;
        }).collect(Collectors.toList());

        return getDataTable(voList,total);
    }
}
